# -*- coding:utf8 -*-
from django.utils import timezone
from wanpuzzle.models import BaseModel

__author__ = 'Forcs'

from django.db import models


class Application(BaseModel):

    code = models.CharField(max_length=6, null=False, default='W0000', unique=True)  # 应用编码
    name = models.CharField(max_length=10, null=False, default='')  # 应用中文名称
    app_id = models.CharField(max_length=30, null=False, default='', unique=True)  # 应用id
    package = models.CharField(max_length=64, null=False, default='')  # 应用包名
    platform = models.CharField(max_length=10, null=True)  # 平台
    token = models.CharField(max_length=32, null=False, default='')
    sign_key = models.CharField(max_length=256, null=False, default='')  # 签名私钥


class Coupon(BaseModel):

    code = models.CharField(max_length=16, null=False, default='C0000', unique=True)  # 优惠券编码
    name = models.CharField(max_length=15, null=False, default='')  # 优惠券中文名称
    money = models.IntegerField(null=False, default=0)   # 优惠券面值
    application = models.ForeignKey(Application, related_name='coupon_app', null=True)  # 对应的应用


class AppVersion(BaseModel):

    version_name = models.CharField(max_length=8, null=False, default='0.0.0')  # 版本名
    version_code = models.CharField(max_length=5, null=False, default='0')  # 版本号
    flavor = models.CharField(max_length=10, null=False, default='unknown')  # 渠道
    date_build = models.CharField(max_length=8, null=False, default='')  # 构建日期
    dl_url = models.URLField(null=False, default='')  # 下载地址
    md5 = models.CharField(max_length=32, null=False, default='')  # md5
    token = models.CharField(max_length=32, null=False, default='')  # 标识
    application = models.ForeignKey(Application, related_name='version_app', null=True)  # 应用
    build_type = models.CharField(max_length=10, null=False, default='release')  # 打包类型 dev;debug;release
    is_force = models.BooleanField(null=False, default=False)  # 是否需要强制更新


class Subject(BaseModel):

    name = models.CharField(max_length=30, null=False, default='')  # 产品包名称
    code = models.CharField(max_length=5, null=False, default='W99', unique=True, db_index=True)  # 产品包代码
    type = models.CharField(max_length=1, null=False, default='U')  # 产品包类型 M：包月，S：季度，F：全年，H：半年，U：未知, A: 单品
    price = models.IntegerField(null=False, default=0)  # 价格
    application = models.ForeignKey(Application, related_name='subject_app', null=True)  # 对应的应用


class AppResVersion(BaseModel):

    version = models.CharField(max_length=32, null=False, default='')  # 版本号
    publish_date = models.DateTimeField(auto_now_add=True)  # 添加时间
    application = models.ForeignKey(Application, related_name='activity_app', null=True)  # 对于的应用


class AppResource(BaseModel):

    name = models.CharField(max_length=64, null=False, default='')  # 文件名
    begin_time = models.DateTimeField(null=True)  # 开始时间
    end_time = models.DateTimeField(null=True)  # 结束时间
    data = models.URLField(null=False, default='')  # 资源数据
    token = models.CharField(max_length=32, null=False, default='')  # 资源标识
    res_type = models.PositiveSmallIntegerField(null=False, default=0)  # 资源类型
    version = models.ForeignKey(AppResVersion, related_name='res_version', null=True)  # 资源版本
